//
//  SetCellView.swift
//  OneMeeting
//
//  Created by zhangheng on 2019/8/23.
//  Copyright © 2019 www.zhangheng.com. All rights reserved.
//

import Foundation
enum SetUIType {
    case `default`
    case radio          //单选
    case multichoice    //多选
}


class SetCellView: NSTableCellView {
    
    var type: SetUIType = .radio
  
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        setupUI()
    }
    
    required init?(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    func selectBtn(_ view:NSView) -> LLCustomBtn {
        let btn = view.viewWithTag(100) as! LLCustomBtn
        return btn
    }
    
    func titleDes(_ view:NSView) -> NSTextField {
        let btn = view.viewWithTag(200) as! NSTextField
        return btn
    }
    
    var convenViews:[NSView] = [NSView]()
    
    public func initconvens(name:[String]) -> () {
        for (index,name) in name.enumerated() {
            let contentview = NSView()
            convenViews.append(contentview)
            self.addSubview(contentview)
            contentview.snp.makeConstraints { (make) in
                make.width.equalToSuperview().offset(-40)
                make.height.equalTo(30)
                make.centerX.equalToSuperview()
                if index == 0 {
                    make.top.equalTo(title.snp.bottom).offset(5)
                }else {
                    make.top.equalTo(convenViews[index-1].snp.bottom).offset(5)
                }
                let selectBtn = LLCustomBtn()
                selectBtn.target = self
                selectBtn.action = #selector(selectBtnAction(_:))
                selectBtn.isHandCursor = true
                selectBtn.tag = 100
                contentview.addSubview(selectBtn)
                selectBtn.snp.makeConstraints { (make) in
                    make.centerY.equalToSuperview()
                    make.left.equalTo(10)
                    make.width.height.equalTo(20)
                }
                
                
                let titleDes = NSLabel.label(title: "播放设置:", textColor: NSColor.black, font: NSFont.systemFont(ofSize: 12))
                titleDes.attributedStringValue = name.subBracketsAttributed(textColor: color_gray(180), font: NSFont.systemFont(ofSize: 12))
                titleDes.tag = 200
                titleDes.sizeToFit()
                contentview.addSubview(titleDes)
                titleDes.snp.makeConstraints { (make) in
                    make.centerY.equalToSuperview()
                    make.left.equalTo(selectBtn.snp.right).offset(5)
                    make.height.equalTo(20)
                }
                
                //设置默认图标
                if self.type == .multichoice {
                    self.setMultichoice(selectBtn)
                }else if self.type == .radio  {
                    self.setRadio(inx: 0)
                }
            }
            
        }
       
    }
    
    func setupUI() -> () {
        self.addSubview(title)
        title.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.left.equalTo(25)
            make.height.equalTo(20)
        }
       
    }
    
    lazy var title: NSLabel = {
        let tef = NSLabel.label(title: "播放设置:", textColor: NSColor.black, font: NSFont.systemFont(ofSize: 12))
        tef.attributedStringValue = "播放设置：（单曲、节目）".attributedT(text: "（单曲、节目）", textColor: color_gray(180), font: NSFont.systemFont(ofSize: 12))
        tef.sizeToFit()
        return tef
    }()
   
    @objc func selectBtnAction(_ senber:LLCustomBtn) -> () {
        
        if type == .radio {
            let cuIndx = self.convenViews.firstIndex(of: senber.superview!)
            self.setRadio(inx: cuIndx!)
        }else if type == .multichoice{
            senber.isSelect = !senber.isSelect
            self.setMultichoice(senber)
        }
       
    }
    
    func setRadio(inx:Int) -> () {
        for (index,view) in convenViews.enumerated() {
            let btn =  view.viewWithTag(100) as! LLCustomBtn
            if index == inx {
                btn.isSelect = false
                btn.defaultBackgroundImage = NSImage.init(named: NSImage.Name.init("ic_chose_seletor"))
            }else {
                btn.isSelect = true
                btn.defaultBackgroundImage = NSImage.init(named: NSImage.Name.init("ic_chose_normal"))
            }
            
        }
        
    }
    
    func setMultichoice(_ selectBtn:LLCustomBtn) -> () {
        if type == .multichoice {
            if selectBtn.isSelect == false {
                selectBtn.defaultBackgroundImage = NSImage.init(named: NSImage.Name.init("ic_michose_normal"))
                selectBtn.selectedBackgroundImage = NSImage.init(named: NSImage.Name.init("ic_michose_seletor"))
            }else {
                selectBtn.defaultBackgroundImage = NSImage.init(named: NSImage.Name.init("ic_michose_seletor"))
                selectBtn.selectedBackgroundImage = NSImage.init(named: NSImage.Name.init("ic_michose_normal"))
            }
           
        }
    }
   
  
}
